/*
 * @Author: lirl
 * @Date: 2022-07-18 10:15:53
 * @Description: 起草功能混入
 */
import { ProcessService } from 'jdf-jssdk';
import routerJump from '@/api/utils/router-jump';
import { getStorageInfo } from '@/api/utils/storage-util';
import { bizServiceURL } from '@/api/utils/constants';
import { showLoading } from '@/utils/utils';
import { showErrorMsg } from '@/api/utils/pc-message';

const draftMixins = {
  methods: {
    /**
     * 起草流程方法
   T * @param {String} templateId 流程模板id
     * @param {String} title 待办标题
     * @param {String} userIdentifyId 起草人身份id，可不传
     * @param {Object} additionalData 附加数据 要通过路由的query传给业务表单的数据，非必传
     * @param {String} to 要跳转到的路由地址
     */
    async draftProcess(templateId, title, userIdentifyId, additionalData = {}, to = '/process-form') {
      const loading = showLoading('流程发起中，请稍候...');
      try {
        const data = await new ProcessService(bizServiceURL).draft({
          identityId: userIdentifyId ?? getStorageInfo('identityId'),
          handlerIdentityId: [],
          processDefinitionKey: templateId,
          title,
        });
        routerJump.jumpToNewWindow(to, {
          processDefinitionKey: templateId,
          taskId: data,
          ...additionalData
        });
      } catch (error) {
        showErrorMsg(error);
      } finally {
        loading.close();
      }
    }
  }
};

export default draftMixins;
